package com.jrx.anytxn.transaction.job.waiveAutoPayment.step;

import com.jrx.anytxn.transaction.entity.TlWaivePaymentLog;
import com.jrx.anytxn.transaction.entity.TlWaivePaymentMapping;
import com.jrx.anytxn.transaction.job.waiveAutoPayment.bean.WaivePaymentLogBean;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlWaivePaymentLogMapper;
import com.jrx.anytxn.transaction.mapper.ext.ExtTlWaivePaymentMappingMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 贴息还款批扣准备writer
 *
 * @author : zhenghaopu
 * 2019/11/22
 */
@Service
public class WaiveAutoPaymentPreWriter implements ItemWriter<WaivePaymentLogBean> {

    @Autowired
    private ExtTlWaivePaymentMappingMapper extTlWaivePaymentMappingMapper;

    @Autowired
    private ExtTlWaivePaymentLogMapper extTlWaivePaymentLogMapper;

    @Override
    public void write(List<? extends WaivePaymentLogBean> items) {
        List<TlWaivePaymentLog> tlPaymentLogs =new ArrayList<>();
        List<TlWaivePaymentMapping> tlPaymentMappings=new ArrayList<>();

        for(WaivePaymentLogBean paymentLogBean:items){
            if(null != paymentLogBean.getTlWaivePaymentLog()){
                tlPaymentLogs.add(paymentLogBean.getTlWaivePaymentLog());
            }
            if(null != paymentLogBean.getTlPaymentMapping()){
                tlPaymentMappings.add(paymentLogBean.getTlPaymentMapping());
            }
        }
        if(!CollectionUtils.isEmpty(items)){
            extTlWaivePaymentLogMapper.batchInsert(tlPaymentLogs);
            extTlWaivePaymentMappingMapper.batchInsert(tlPaymentMappings);
        }
    }
}
